#include "LMS.h"

LMS::LMS(shared_ptr<CRossenblatt> network, function<double(int)> backfireFunction, double ConvergenceThreshold) :
	m_pNetwork(network),
	m_ConvergenceThreshold(ConvergenceThreshold),
	m_BackFireFunction(backfireFunction)
{
}

void LMS::Training(Vector Input, Vector ExceptOutput)
{
	int N = 1;

	Vector curOutput(1);

	while (true)
	{
		curOutput = this->m_pNetwork->Execute(Input);

		double error = ExceptOutput[0] - curOutput[0];
		
		if (fabs(error) <= this->m_ConvergenceThreshold)
		{
			break;
		}
		
		Vector curWeight = this->m_pNetwork->m_Neuron.GetWeightVector();
		
		Vector curInput(curWeight.GetDimesion());
		curInput = curInput << Input;
		curInput[curWeight.GetDimesion() - 1] = 1.0;

		Vector newWeight = curWeight + curInput * error * this->m_BackFireFunction(N);

		this->m_pNetwork->m_Neuron.SetWeightVector(newWeight);

		N++;
	}
}